package com.ipay.wechat.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ipay.common.Base64Coder;
import com.ipay.utils.MD5;
import com.ipay.utils.MapUtils;
import com.ipay.utils.Query;
import com.ipay.utils.R;
import com.ipay.wechat.pojo.Bills;
import com.ipay.wechat.pojo.BillsDetails;
import com.ipay.wechat.pojo.MerchantInfo;
import com.ipay.wechat.pojo.TradeRecord;
import com.ipay.wechat.service.ApplyService;
import com.ipay.wechat.service.BillsService;
import com.ipay.wechat.service.TradeRecordService;

@Controller
public class TradeController {
	private Logger logger=LoggerFactory.getLogger(getClass());
	@Autowired
	private TradeRecordService trService;
	@Autowired
	private ApplyService applyService;
	@Autowired
	private BillsService billsService;
	
	@RequestMapping(value="/viewTrade")
	public ModelAndView viewTradeRecord(){
		
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("busId", "123");
		map.put("page", 1);
		map.put("limit", 3);
		map.put("sidx", "");
		map.put("order", "");
		Query query = new Query(map);
		List<TradeRecord> tradeRecordList=trService.findTradeRecordList(query);
		logger.info("size=="+tradeRecordList.size());
		return new ModelAndView("/applysuccess.jsp");
	}
	
	@RequestMapping(value="/viewTradeDetails")
	public ModelAndView viewTradeDetails(@RequestParam String out_trade_no,ModelMap map){
		TradeRecord tradeDetails=trService.findTradeRecordDetails(out_trade_no);
		MerchantInfo info=applyService.querryMerchantInfoByMerchantNo(tradeDetails.getBusId());
		map.put("info", info);
		return new ModelAndView();
	}
	
	@ResponseBody
	@RequestMapping(value="/getBills")
	public void getBills(@RequestParam String time){
		//day
		Bills bills=billsService.queryObject();
		billsService.queryBillsDetails(bills.getBills_id());
	}
	
	@RequestMapping(value = "/auth/payQuery", method = RequestMethod.POST)
	public R payQuery(HttpServletRequest request) throws Exception {
		String mchPayKey="c45f4006f96776b53ed13e341eb49306";
		
		String sign = request.getParameter("sign");//签名
		String sponsorOrderNo = request.getParameter("sponsorOrderNo");//发起方订单号
		String merchantNo = request.getParameter("merchantNo");//商户号
		
		Map<String,String> map = new HashMap<>();
		map.put("sponsorOrderNo",sponsorOrderNo);//发起方订单号
		map.put("merchantNo",merchantNo);//商户号
		String strtosign = MapUtils.formatUrlMap(map, false, false);
		String parameter= Base64Coder.encode(strtosign);
		String signstr = MD5.sign(parameter, "&payKey="+mchPayKey , "utf-8");
		System.out.println("****************signstr***********************:"+signstr);
		if(!signstr.equals(sign)){
			return R.error("签名错误");
		}
		TradeRecord tradeRecord = trService.queryByParam(map);
		if(tradeRecord != null){
			return R.ok().put("status", tradeRecord.getStatus());
		}
		return R.error("参数异常");
	}
}
